﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;


namespace IronOne.SalesOne.DataObjects
{
    public class LinqLifeBenefit
    {

        public int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]);
        public List<QuoLifeBenefit> GetBenefits(string sortExpression, int page)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<QuoLifeBenefitEntity> query = db.QuoLifeBenefitEntities;
                var pageData = query.Skip((page - 1) * pageSize).Take(pageSize);
                return pageData.Select(c => Mapper.ToBenefitObject(c)).ToList();
            }
        }

        public List<QuoLifeBenefit> GetBenefitsD(string sortExpression)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<QuoLifeBenefitEntity> query = db.QuoLifeBenefitEntities;
                
                return query.Select(c => Mapper.ToBenefitObject(c)).ToList();
            }
        }

        public List<QuoLifeBenefit> GetBenefit(string benefitCode, int page)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                ////////////////////////////

                //using (SODataDataContext db = DataContextFactory.CreateContext())
                //{
                //    //IQueryable<GroupEntity> query = db.GroupEntities;

                //    var query = from c in db.GroupEntities
                //                where SqlMethods.Like(c.GroupName, name)
                //                select c;

                //    var pageData = query.Skip((pageNo - 1) * pageSize).Take(pageSize);

                //    return pageData.Select(c => Mapper.ToGroupObject(c)).ToList();
                //}

                //////////////////////////

                var query = from benefit in db.QuoLifeBenefitEntities
                            where benefit.BenefitCode == benefitCode
                            select benefit;

                var pageData = query.Skip((page - 1) * pageSize).Take(pageSize);

                return pageData.Select(c => Mapper.ToBenefitObject(c)).ToList();

            }
        }

        public QuoLifeBenefit GetOneBenefit(string benefitCode)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToBenefitObject(db.QuoLifeBenefitEntities
                            .SingleOrDefault(p => p.BenefitCode == benefitCode));
            }
        }

        public int UpdateBenefit(QuoLifeBenefit benefit)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    QuoLifeBenefitEntity entity = Mapper.ToBenefitEntity(benefit);
                    db.QuoLifeBenefitEntities.Attach(entity, true);
                    db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to update code record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }

    }
}
